package com.sdp.wx.handleImpl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sdp.core.bizc.utils.ApplicationContextUtils;
import com.sdp.core.bizc.vo.BusinessContext;
import com.sdp.wx.controller.WxRepairCodeController;
import com.sdp.wx.db.wx_wz_arrival.entity.WxWzArrival;
import com.sdp.wx.db.wx_wz_arrival.service.IWxWzArrivalService;
import com.sdp.wx.db.wx_wz_arrival_detail.entity.WxWzArrivalDetail;
import com.sdp.wx.db.wx_wz_arrival_detail.service.IWxWzArrivalDetailService;
import com.sdp.wx.db.wx_wz_procured.entity.WxWzProcured;
import com.sdp.wx.db.wx_wz_procured.service.IWxWzProcuredService;
import com.sdp.wx.db.wx_wz_procured_detail.entity.WxWzProcuredDetail;
import com.sdp.wx.db.wx_wz_procured_detail.service.IWxWzProcuredDetailService;
import com.sdp.wx.handle.WzArrivalHandle;
import com.sdp.wx.utils.WxConstants;


@Component
public class WzArrivalHandleImpl extends WzArrivalHandle{
	
	@Autowired
	private IWxWzProcuredService iWxWzProcuredService;
	
	@Autowired
	private IWxWzProcuredDetailService iWxWzProcuredDetailService;
	
	@Autowired
	private IWxWzArrivalService iWxWzArrivalService;
	
	@Autowired
	private IWxWzArrivalDetailService iWxWzArrivalDetailService;

	@Autowired
	private WxRepairCodeController WxRepairCodeController;
	
	/**
	 * 不能在此方法上使用事务，会不生效
	 */
	@Override
	public BusinessContext execute(BusinessContext businessContext) {
		return ApplicationContextUtils.getBean(WzArrivalHandleImpl.class).wzArrival(businessContext);
	}

	@Transactional(rollbackFor=Exception.class)
	public BusinessContext wzArrival(BusinessContext businessContext) {
		List<WxWzArrivalDetail> wzList = businessContext.get(WxWzArrivalDetail.class);
		WxWzArrival wxWzArrival = businessContext.get(WxWzArrival.class);
		
		String planCode = wxWzArrival.getPlanCode();
		if(StringUtils.isEmpty(planCode)) {
			businessContext.setMessage("采购信息不存在");
			businessContext.setSuccess(false);
			return businessContext;
		}
		//先查询此到货单是否已经完成到货
		WxWzProcured wxWzProcured = iWxWzProcuredService.queyrWxWzProcuredByCode(planCode);
		if(wxWzProcured==null) {
			businessContext.setMessage("采购信息不存在");
			businessContext.setSuccess(false);
			return businessContext;
		}
		if(WxConstants.是.getCode().equals(wxWzProcured.getIsArrival())) {
			businessContext.setMessage("此采购单已经完成到货，不能重复到货");
			businessContext.setSuccess(false);
			return businessContext;
		}
		
	
		List<WxWzProcuredDetail> wzProcuredDetailList = iWxWzProcuredDetailService.queryCanArrivalWzProcuredDetailByPlanCode(planCode);
		if(wzProcuredDetailList==null || wzProcuredDetailList.size()==0){
			businessContext.setMessage("此采购单中的器材已全部到货，请确认");
			businessContext.setSuccess(false);
			return businessContext;
		}
		
		if(wzProcuredDetailList.size()!=wzList.size()) {
			businessContext.setMessage("此采购单中的器材到货信息有变动，请先确认");
			businessContext.setSuccess(false);
			return businessContext;
		}
		
		Map<String,WxWzArrivalDetail> wzArrivalDetailMap = new HashMap<>();
		for(WxWzArrivalDetail wzArrivalDetail : wzList) {
			wzArrivalDetail.setProcuredDetailId(wzArrivalDetail.getKeyId());
			wzArrivalDetail.setKeyId(null);
			wzArrivalDetailMap.put(wzArrivalDetail.getProcuredDetailId(), wzArrivalDetail);
		}
		
		boolean falg = false;
		for(WxWzProcuredDetail wzProcuredDetail : wzProcuredDetailList) {
			if(wzArrivalDetailMap.containsKey(wzProcuredDetail.getKeyId())) {
				WxWzArrivalDetail arrivalWz = wzArrivalDetailMap.get(wzProcuredDetail.getKeyId());
				BigDecimal arrivalNum = arrivalWz.getArrivalNum();
				if(wzProcuredDetail.getNotArrivalNum().compareTo(arrivalWz.getArrivalNum())<0) {
					//如果采购单中的未到货数量小于到货单中的到货数量，说明数据不合法
					falg = true;
					break;
				}else {
					//如果有不合格的
					if(WxConstants.否.getCode().equals(arrivalWz.getIsQualified())) {
						//到货数量 应该要减去不合格数量
						arrivalNum = arrivalNum.subtract(arrivalWz.getUnqualifiedNum());
					}
					//采购单中的到货信息增加
					wzProcuredDetail.setArrivalNum(wzProcuredDetail.getArrivalNum().add(arrivalNum));
					//采购单中的未到货信息减少
					wzProcuredDetail.setNotArrivalNum(wzProcuredDetail.getNotArrivalNum().subtract(arrivalNum));
				}
			}else {
				falg = true;
				break;
			}
		}
		
		if(falg) {
			businessContext.setMessage("此采购单中的器材到货信息有变动，请先确认");
			businessContext.setSuccess(false);
			return businessContext;
		}
		
		//更新采购单信息
		iWxWzProcuredDetailService.updateBatchById(wzProcuredDetailList);
		//判断采购单中的物资是否都已经全部到货
		wzProcuredDetailList = iWxWzProcuredDetailService.queryWxWzProcuredDetailByPlanCode(planCode);
		for(WxWzProcuredDetail wzProcuredDetail : wzProcuredDetailList) {
			if(wzProcuredDetail.getNotArrivalNum().compareTo(BigDecimal.ZERO)>0) {
				falg=true;
			}
		}
		if(!falg) {
			wxWzProcured.setIsArrival(WxConstants.是.getCode());
			iWxWzProcuredService.updateById(wxWzProcured);
			
		}
		
		//添加到货单信息
		wxWzArrival.setArrivalCode(WxRepairCodeController.getCode(WxConstants.流水号前缀_器材到货单.getCode(), WxConstants.流水号类型_器材到货单.getCode()));
		wxWzArrival.setCheckTime(LocalDateTime.now());
		wxWzArrival.setIsInput(WxConstants.否.getCode());
		wxWzArrival.setCreateTime(LocalDateTime.now());
		iWxWzArrivalService.save(wxWzArrival);
		for(WxWzArrivalDetail wzArrivalDetail : wzList) {
			wzArrivalDetail.setArrivalCode(wxWzArrival.getArrivalCode());
			wzArrivalDetail.setArrivalId(wxWzArrival.getKeyId());
			wzArrivalDetail.setCheckPerson(wxWzArrival.getCheckPerson());
			wzArrivalDetail.setCheckTime(LocalDateTime.now());
			wzArrivalDetail.setCreateTime(LocalDateTime.now());
		}
		iWxWzArrivalDetailService.saveBatch(wzList);
		
		return businessContext;
	}
}
